package com.cxkj.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cxkj.dto.Users;
import com.cxkj.service.UserService;
import com.cxkj.tools.MD5Tools;

@Controller
@RequestMapping("/user")
public class UserController {
	
	@Autowired
	private UserService userService;
	/**
	 * 用户登录
	 * @param user
	 * @param model
	 * @param request
	 * @param response
	 * @param session
	 * @return
	 */
	@RequestMapping("/userLogin")
	public String userLogin(Users user,Model model,HttpServletRequest request,HttpServletResponse response,HttpSession session){
		System.out.println(MD5Tools.MD5(user.getPassword()));
		Users users = userService.userLogin(user.getUsername(), MD5Tools.MD5(user.getPassword()));
		if(users == null){
			model.addAttribute("result", "登录失败，用户名或者密码错误。");
			return "login";
		}else{
			//记住用户名、密码功能
	         String remFlag = request.getParameter("remFlag");
	         if("1".equals(remFlag)){ //"1"表示用户勾选记住密码
	             String loginInfo = user.getUsername()+","+user.getPassword();
	             Cookie userCookie=new Cookie("loginInfo",loginInfo); 
	             userCookie.setMaxAge(7*24*60*60);   //存活期为七天 7*24*60*60
	             userCookie.setPath("/");
	             response.addCookie(userCookie); 
	         }
	         session.setAttribute("user", users);
	         model.addAttribute("user", users);
	         if(!user.getUsername().equals(users.getUsername()) && !user.getPassword().equals(users.getPassword())){
					model.addAttribute("result", "对不起，您没有权限登录后台管理系统。");
					return "login";
				}
			
		}
		
		
		return "document-input";
	}

	/**
	 * 用户退出登录
	 */
	@RequestMapping("/userLoginOut")
	public String userLoginOut(HttpSession session){
		 
		session.invalidate();
		//返回到登录页面
		return "login";
		
	}
	/**
	 * 转至添加用户页面
	 * @return
	 */
	@RequestMapping("/addUserJsp")
	public String addUserJsp(){
		
		return "add-ID";
	}
	/**
	 * 添加用户
	 * @param users
	 * @return
	 */
	@RequestMapping("/insertUser")
	public String insertUser(Users users,Model model){
		
		userService.insertUser(users,model);
		
		return "add-ID";
	}
	/**
	 * 添加账号重复检测
	 * @param username
	 * @param response
	 * @return
	 */
	@RequestMapping("/userTest")
	@ResponseBody
	public Map<String,Object> userTest(String username,HttpServletResponse response){
		response.setCharacterEncoding("UTF-8");
		List<Users> list = userService.nameTest(username);
		Map<String, Object> result=new HashMap<String, Object>();
		if(list.size() == 0 ){
			result.put("result", "0");
		}else{
			result.put("result", "1");
		}
		return result;
	}
	
}
